package com.yanfan.dlt698_45.frame;


import com.yanfan.dlt698_45.code.DestinationCode;
import com.yanfan.dlt698_45.code.FrameType;
import com.yanfan.dlt698_45.code.FunctionCode;
import com.yanfan.dlt698_45.exception.DLT698_45TransportException;
import io.netty.buffer.ByteBuf;

/**
 * @author oliver foo
 * @description
 * @date 2023/3/21 14:11
 */
abstract public class SecurityRequest extends DLT698_45Frame {
    public SecurityRequest(byte[] address, byte addressType) throws DLT698_45TransportException {
        super(address, addressType);
    }

    @Override
    public byte getDestination() {
        return DestinationCode.CLIENT_REQUEST;
    }

    @Override
    public boolean hasFollowFrame() {
        return false;
    }


    @Override
    public byte getFunctionCode() {
        return FunctionCode.USER_DATA;
    }

    @Override
    public byte getClientAddress() {
        return 0;
    }

    abstract int getRequestLength();


    abstract void writeRequest(ByteBuf buf);

    abstract byte[] getMac();


    @Override
    protected void writeImpl(ByteBuf buf) {
        //SECURITY-Request
        buf.writeByte(FrameType.SECURITY_REQUEST);
        // 必须是明文传输（1 代表密文传输）
        buf.writeByte(0);
        // 明文长度
        buf.writeByte(getRequestLength());
        // 写入信息
        writeRequest(buf);
        // 数据验证信息 (0数据验证码  1 随机数)
        buf.writeByte(1);
        // MAC 地址长度
        buf.writeByte(getMac().length);
        // MAC地址
        buf.writeBytes(getMac());
    }


}
